<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
        list-style: none;
        border: 0;
      }
      .all {
        width: 500px;
        height: 200px;
        padding: 7px;
        border: 1px solid #ccc;
        margin: 100px auto;
        position: relative;
      }
      .screen {
        width: 500px;
        height: 200px;
        overflow: hidden;
        position: relative;
      }
      .screen li {
        width: 500px;
        height: 200px;
        overflow: hidden;
        float: left;
      }
      .screen ul {
        position: absolute;
        left: 0;
        top: 0px;
        width: 3000px;
      }
      .all ol {
        position: absolute;
        right: 10px;
        bottom: 10px;
        line-height: 20px;
        text-align: center;
      }
      .all ol li {
        float: left;
        width: 20px;
        height: 20px;
        background: #fff;
        border: 1px solid #ccc;
        margin-left: 10px;
        cursor: pointer;
      }
      .all ol li.current {
        background: rgb(108, 78, 33);
      }
      /*#arr {display: none;}*/
      #arr span {
        width: 40px;
        height: 40px;
        position: absolute;
        left: 5px;
        top: 50%;
        margin-top: -20px;
        background: #000;
        cursor: pointer;
        line-height: 40px;
        text-align: center;
        font-weight: bold;
        font-family: "黑体";
        font-size: 30px;
        color: #fff;
        opacity: 0.3;
        border: 1px solid #fff;
      }
      #arr #right {
        right: 5px;
        left: auto;
      }
    </style>
  </head>
  <body>
    <div class="all" id="all">
      <div class="screen" id="screen">
        <ul id="ul">
          <li><img src="./images/1.webp" width="500" height="200" /></li>

          <li><img src="./images/2.webp" width="500" height="200" /></li>

          <li><img src="./images/3.webp" width="500" height="200" /></li>

          <li><img src="./images/4.webp" width="500" height="200" /></li>

          <li><img src="./images/5.webp" width="500" height="200" /></li>
        </ul>

        <ol></ol>
        <div id="arr"><span id="left"><</span> <span id="right">></span></div>
      </div>
    </div>

    <script>
      window.onload = function () {
        var ul = document.querySelector("ul");
        var ol = document.querySelector("ol");
        var prev = document.getElementById("left");
        var next = document.getElementById("right");
        var pointIndex = 0; //默认显示第一个圆点
        var picIndex = 0;

        // 根据图片的数量生成指定的圆点
        for (var i = 0; i < ul.children.length; i++) {
          var point = document.createElement("li"); //创建圆点标签
          point.innerHTML = i + 1; //给圆点设置图片序号
          ol.appendChild(point); //添加到原点容器中
        }
        var newLi = ul.children[0].cloneNode(true);
        ul.appendChild(newLi);
        var timer = setInterval(autoplay, 1000);
        light();
        //上一张
        prev.onclick = function () {
          pointIndex--;
          picIndex--;
          // 图片边界处理
          if (pointIndex < 0) {
            // ul.style.left='-2000px'
            ul.style.left = -(ul.children.length - 1 - 1) * 500 + "px";
            pointIndex = ul.children.length - 1 - 1;
            picIndex = ul.children.length - 1 - 1;
          }
          //圆点边界处理
          pointIndex = pointIndex < 0 ? ol.children.length - 1 : pointIndex;

          animate(ul, { left: -500 * picIndex });
          light();
        };
        //自动播放
        next.onclick = autoplay;

        //鼠标移入移出时间

        var screen = document.getElementById("screen");
        // 移出停止定时器
        screen.onmouseenter = function () {
          clearInterval(timer);
        };
        // 重新设置定时器
        screen.onmouseleave = function () {
          clearInterval(timer);
          timer = setInterval(autoplay, 1000);
        };
        //下一张
        function autoplay() {
          pointIndex++;
          picIndex++;
          // 图片边界处理
          if (picIndex > ul.children.length - 2) {
            //图片索引重置为0
            pointIndex = 0;
            //圆点索引重置为0
            picIndex = 0;
            //显示第一张图片
            ul.style.left = 0;
          } else {
            animate(ul, { left: -500 * picIndex });
          }
          //圆点边界处理
          pointIndex = pointIndex > ol.children.length - 1 ? 0 : pointIndex;
          // pointIndex > ol.children.length - 1 && (pointIndex = 0);
          light();
        }
        // 默认第一个圆点高亮
        ol.children[pointIndex].className = "current";
        function light() {
          // 排他思想
          for (var i = 0; i < ol.children.length; i++) {
            //取消所有圆点的样式
            ol.children[i].className = "";
          }
          // 默认第一个圆点高亮
          ol.children[pointIndex].className = "current";
        }

        // 返回指定dom节点的attr样式值
        function getStyle(dom, attr) {
          if (window.getComputedStyle) {
            // 是否有getComputedStyle方法
            //非IE低版
            return window.getComputedStyle(dom, null)[attr];
          } else {
            //IE低版
            return dom.currentStyle[attr];
          }
        }

        // 封装一个缓动的动画函数，需要时调用即可
        function animate(dom, json, callback) {
          /* 
       dom:要运动的节点
       json:要运动的样式名
       callback:运动完成的回调函数
   */ //要用定时器，先清定时器 //要运动的定时器
          clearInterval(dom.timer);
          dom.timer = setInterval(function () {
            //json有几个属性，就要运动几次
            var flag = true;
            for (var attr in json) {
              //获取当前位置点击right按钮，看下一张，点亮小圆点
              // 计算速度
              var current = parseInt(getStyle(dom, attr));
              var speed =
                json[attr] - current > 0
                  ? Math.ceil((json[attr] - current) / 10)
                  : Math.floor((json[attr] - current) / 10); //计算下一个位置
              //定位元素
              var next = current + speed;
              //判断是否达到目标
              dom.style[attr] = next + "px";
              if (next != json[attr]) {
                flag = false;
              }
            }
            if (flag) {
              clearInterval(dom.timer);
              //为了防止造成不传造成当前函数调用的报错
              callback && callback();
            }
          }, 20); // 每20毫秒
        }
      };
    </script>
  </body>
</html>
